// Copyright 2009 StarCite Inc.  All rights reserved.
// SynDirectory.java 5:00:53 PM danny.liu
package com.billowsoft.syncer.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author danny.liu Nov 28, 2011
 * @version 1.0
 * 
 */
public class SyncDirectory {

	private String path;
	private String parentPath;
	private List<SyncObject> subDirectories;

	/**
	 * @param parentDir
	 * @param isDir
	 */
	public SyncDirectory(String path) {
		setPath(path);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
		this.onBasePathSet();
	}

	private void onBasePathSet() {
		File file = new File(this.path);
		if (!file.exists() || !file.isDirectory()) {
			throw new RuntimeException("Invalid direcotry with path: "
					+ path);
		}

		this.subDirectories = new ArrayList<SyncObject>();
		File[] subFiles = file.listFiles();
		for (File subFile : subFiles) {
			subDirectories.add(new SyncObject(getSubFilePath(subFile.getPath()), subFile
					.isDirectory(), new Date(subFile.lastModified())));
		}
		
		this.parentPath = file.getParent();
	}
	
	private String getSubFilePath(String subFilePath){
		return subFilePath.replace(this.path, "");
	}

	public List<SyncObject> getSubDirectories() {
		return subDirectories;
	}

	public String getParentPath() {
		return parentPath;
	}

}
